/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uk.ac.cam.juliet.zedcat.client;

import java.sql.SQLException;
import java.sql.Time;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import uk.ac.cam.juliet.zedcat.db.ImageAnalysisDatabase;
import uk.ac.cam.juliet.zedcat.db.ImageHotspot;

/**
 *
 * @author Chris
 */
// Wrapper class for database communication.
public class Database {

    // Store a copy of the db, last config update, and a cache of hotspots.
    private ImageAnalysisDatabase db;
    private Date lastUpdate;
    private List<Hotspot> hotspot_cache;

    private String ExitID, EntryID;

    // Connect to the database on construction.
    public Database(String username, String password, String location) {
        try {
            this.db = new ImageAnalysisDatabase(username, password, location);
            connect();
            lastUpdate = new Time(0);
            hotspot_cache = new LinkedList<Hotspot>();
            ExitID = db.exitID();
            EntryID = db.entryID();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // Cached hotspot recall.
    // If the time is greater than 5 minutes since the last fetch, do a query
    // otherwise return the cache.
    public List<Hotspot> fetchHotspots(Configuration c) {
        Date now = new Date();
        long time_since = now.getTime() - lastUpdate.getTime();
        if (time_since < 1000*60*5) // 5 minutes
            return hotspot_cache;
        try {
            List<ImageHotspot> dbhotspots = db.getHotspots();
            hotspot_cache.clear();
            // Translate from coordinates from server into a Rectangle suited for our source.
            for (ImageHotspot h : dbhotspots) {
                double scalefactor = ((double) c.ImageWidth) / 320;
                // System.out.println("Client width: " + c.ImageWidth + ", scale factor: " + scalefactor);
                Hotspot nh = new Hotspot((int) Math.round(scalefactor * (h.getX2()-h.getX1())), // width
                                         (int) Math.round(scalefactor * (h.getY2()-h.getY1())), // height
                                         (int) Math.round(scalefactor * h.getX1()), // top left x
                                         (int) Math.round(scalefactor * h.getY1()), // top left y
                                         h.getId());
                hotspot_cache.add(nh);
            }
            lastUpdate = now;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return hotspot_cache;
    }

    // Method used to report an entry event.
    public void reportEntry(int hotspotId, String imageurl) {
        try {
            db.addSighting(EntryID, (long) hotspotId, imageurl);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // Method used to report an exit event.
    public void reportExit(int hotspotId, String imageurl) {
        try {
            db.addSighting(ExitID, (long) hotspotId, imageurl);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    // Database connection method.
    public void connect() {
        try {
            db.connect();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
